require "Common/define"
require "Logic/Config"

UIActivity = {};
HelperFunc.SetUIEnv(UIActivity)
local this = UIActivity;

this.gameObject = nil;
this.hasInit = false;
this.allActiveTypeData = {{type=EnumConst.ActiveShowType.TimeLimit},
                          {type=EnumConst.ActiveShowType.Forever},
                          {type=EnumConst.ActiveShowType.ActiveCenter}};

function UIActivity.Init()
    local name, packageName = getUIName(PanelNames.UIActivity);
    panelMgr:ShowUI(name, packageName, this.OnCreate, Config.useLocalUI);
end

function UIActivity.OnCreate(obj)
    this.gameObject = obj;
    this.gameObject.name = "UIActivity";
   -- AddUIPackageDepend(PanelNames.UIActivity,"FUI/BusinessAtlas1")
   -- AddUIPackageDepend(PanelNames.UIActivity,"FUI/BusinessAtlas2")
   -- AddUIPackageDepend(PanelNames.UIActivity,"FUI/BusinessAtlas3")
    if not this.hasInit then
        this.hasInit = true;
        this.Backbtn = obj:GetChild("Backbtn")
        this.content = obj:GetChild("list");
        --this.backGround = obj:GetChild("backGround");
        --this.activeTime = obj:GetChild("time");
        --this.awardTime = obj:GetChild("time2");
        --this.enterbtn = obj:GetChild("enterbtn");
        this.CashLb = obj:GetChild("CashLb");
        this.CoinLb = obj:GetChild("CoinLb");
        this.Tab_BtnTime = obj:GetChild("Tab_BtnTime");
        this.Tab_BtnForever = obj:GetChild("Tab_BtnForever");
        this.Tab_BtnAll = obj:GetChild("Tab_BtnAll");
        this.allActiveTypeData[1].btn = this.Tab_BtnTime;
        this.allActiveTypeData[2].btn = this.Tab_BtnForever;
        this.allActiveTypeData[3].btn = this.Tab_BtnAll;
        this.addClick();
    end
    this.playEffect = true;
    this.lastIndex = nil;
    this.isPlaying = nil;
    this.nowIndex = 1;
    this.cheackActiveTab();
end

function UIActivity.OnEnable()
    this.LocalListenEvent();
    this.needShowHeadMenu = nil;
    if isOpenUI(PanelNames.UIHeadMenu) then
        this.needShowHeadMenu = true;
        closeUI(PanelNames.UIHeadMenu);
    end
    if this.timeCountDown == nil then
        this.timeCountDown = utimer.timeCall(this.updataActiveCountDown, 1000, -1);
    end
end

function UIActivity.OnDisable()
    this.LocalCloseEvent();
    if this.timeCountDown ~= nil then
        utimer.remove(this.timeCountDown);
        this.timeCountDown = nil;
    end
    this.item_list_str = nil
end

function UIActivity.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_Currency, this.updateResource);
    ListenEvent(Config.EventType.Fresh_Active, this.refreshActivity);
end

function UIActivity.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_Currency, this.updateResource);
    CloseEvent(Config.EventType.Fresh_Active, this.refreshActivity);
end

function UIActivity.OnDestroy()
    this.hasInit = false;
    this.removeClick()
end

function UIActivity.updateResource()
    this.CashLb.icon = "ui://ItemAtlas/3";
    this.CoinLb.icon = "ui://ItemAtlas/1";
    local _currentGoldStock = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.MONEY)
    local _currentDiamondStock = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.GOLD)

    this.CashLb.title = getNumString(_currentGoldStock);
    this.CoinLb.title = _currentDiamondStock;
end

function UIActivity.addClick()
    this.Backbtn.onClick:Add(this.onClickBack);
    --this.enterbtn.onClick:Add(this.onClickEnter);
    this.content.onClickItem:Add(this.onClickItem);
    for k, v in pairs(this.allActiveTypeData) do
        v.btn.onClick:Add(function ()
            this.onClickTab(k);
        end);
    end
end

function UIActivity.removeClick()
    this.Backbtn.onClick:Remove(this.onClickBack);
    --this.enterbtn.onClick:Remove(this.onClickEnter);
    this.content.onClickItem:Remove(this.onClickItem);
    for k, v in pairs(this.allActiveTypeData) do
        v.btn.onClick:Clear();
    end
    UIRedDots.removeViewEvent(PanelNames.UIActivity)
end

function UIActivity.refreshActivity()
    this.playEffect = false;
    this.cheackActiveTab();
end

function UIActivity.cheackActiveTab()
    local showNum = 0;
    local fistType = 0;
    UIRedDots.removeViewEvent(PanelNames.UIActivity)
    for k, v in pairs(this.allActiveTypeData) do
        local data = logicMgr.ActiveManager.getActiveEntranceDataListByType(v.type);
        if #data > 0 then
            v.btn.selected = false;
            v.btn.visible = true;
            if showNum == 0 then
                showNum = k;
            else
                showNum = -1;
            end
            if this.activeType == v.type then
                v.btn.selected = true;
                fistType = -1;
            end
            if fistType == 0 then
                fistType = k;
            end
            UIRedDots.addViewListen(PanelNames.UIActivity, "ActiveTab" .. v.type, v.btn, Config.RedDotsType.ActivityCentre, nil, v.type);
        else
            v.btn.visible = false;
        end
    end
    if showNum > 0 then
        this.allActiveTypeData[showNum].btn.visible = false;
        UIRedDots.removeViewEvent(PanelNames.UIActivity)
    end
    if fistType > 0 then
        this.allActiveTypeData[fistType].btn.selected = true;
        this.activeType = this.allActiveTypeData[fistType].type;
    end
    this.initActivity(this.activeType);
end

function UIActivity.initActivity(type)
    this.activeType = type or this.activeType or EnumConst.ActiveShowType.ActiveCenter;
    this.updateResource();
    this.ActiveBtnTab = logicMgr.ActiveManager.getActiveEntranceDataListByType(this.activeType);

    --活动列表中去除不显示的98累充和先知召唤筛选
    local temp={}
    local removeTab={[11004]=true}
    --local specialDraws={10402,10403,10404}
    --for i,v in ipairs(specialDraws)do
    --    local _,serverData=logicMgr.ActiveManager.getActiveData(v)
    --    if not table.isNullOrEmpty(serverData)then
    --        if serverData.activeState==SpecialDrawState.canNotBuy then
    --            removeTab[v]=true
    --        end
    --    else
    --        removeTab[v]=true
    --    end
    --end
    for i,v in pairs(this.ActiveBtnTab)do
        if not removeTab[v.activeId] then
            if v.activeType == nil or tonumber(v.activeType) ~= 7 then
                table.insert(temp,v)
            else
                local _,serverData=logicMgr.ActiveManager.getActiveData(v.activeId)
                if serverData ~= nil and serverData.activeState~=SpecialDrawState.canNotBuy then
                    table.insert(temp,v)
                end
            end
        end
    end
    this.ActiveBtnTab=temp

    local num = #this.ActiveBtnTab;
    if this.nowIndex > num then
        this.nowIndex = num;
    end
    this.itemTimeList = {};
    this.content.itemRenderer = this.initTop;
    this.content.numItems = num;
    if this.playEffect and not this.isPlaying then
        this.isPlaying = true;
        HelperFunc.ListTranstionPlay(this.content,function()
            this.scrollToView();
            this.isPlaying = false;
        end)
    else
        this.scrollToView();
    end
end

function UIActivity.scrollToView()
    if this.nowIndex > 0 then
        this.content:ScrollToView(this.nowIndex - 1);
    end
end

function UIActivity.initTop(index, obj)
    local data = this.ActiveBtnTab[index + 1];
    local icon = obj:GetChild("icon");
    local frame = obj:GetChild("frame");
    local time = obj:GetChild("time");
    local titleTop = obj:GetChild("titleTop");
    icon.url = tostring(data.titlePic);
    frame.url = tostring(data.backgroundPic);
    titleTop.text = data.title
    --每日充值活动特判
    if data.activeId==11003 then
        local _,temp=logicMgr.ActiveManager.disposeDailyChargeServerData(data.activeId)
        data.endTime=temp.endTime
    end

    if data.activeId==11005 then
        local _,temp=logicMgr.ActiveManager.disposeLayerChargeData(data.activeId)
        data.endTime=temp.endTime
    end

    if data.showTimeType == 1 then
        local lastTime = data.endTime - HelperFunc.GetServerTime();
        if lastTime <= 0 then
            time.text = getLanguage("Sys_ActivityEnd")
        else
            time.text = string.format(getLanguage("Sys_ActivityTimeLeft"), HelperFunc.GetSurpTimeStr(lastTime)); --os.date("%Y年%m月%d日%H点-", data.startTime)..os.date("%Y年%m月%d日%H点", data.endTime);
            local dataTime = {};
            table.insert(dataTime, time);
            table.insert(dataTime, lastTime);
            table.insert(this.itemTimeList, dataTime);
        end
    else
        time.text = getLanguage(data.titleTips);
    end
    UIRedDots.addViewListen(PanelNames.UIActivity, this.gameObject.name .. index, obj, data.redDots, data.activeId);
end

--function UIActivity.updateBackGround(index)
    --local data = this.AcitveBtnTab[index];
    --this.backGround.url = "ui://UIActivity/" .. data.backgroundPic;
    --if data.showTimeType == 1 then
        --local time = HelperFunc.GetServerTime();
        --this.endCountDown = data.endTime - time;
        --this.collectCountDown = data.collectTime - time;
        --this.updataActiveCountDown();
    --else
        --this.endCountDown = nil;
        --this.collectCountDown = nil;
        --this.activeTime.text = "";
        --this.awardTime.text = "";
    --end
--end

function UIActivity.updataActiveCountDown()
    if this.itemTimeList == nil then
        return
    end
    if this.item_list_str == nil then
        this.item_list_str = {}
    end
    for k, v in pairs(this.itemTimeList) do
        v[2] = v[2] - 1;
        local tip_str
        if v[2] <= 0 then
            tip_str = getLanguage("Sys_ActivityEnd");
        else
            tip_str = string.format(getLanguage("Sys_ActivityTimeLeft"), HelperFunc.GetSurpTimeStr(v[2]));
        end
        if this.item_list_str[k] == nil or this.item_list_str[k] ~= tip_str then
            v[1].text = tip_str
            this.item_list_str[k] = tip_str
        end
    end
    --if this.endCountDown == nil or this.collectCountDown == nil then
    --    return;
    --end
    --if this.endCountDown <= 0 then
    --    this.activeTime.text = "已结束";
    --else
    --    this.activeTime.text = HelperFunc.GetSurpTimeStr(this.endCountDown);
    --end
    --this.endCountDown = this.endCountDown - 1;
    --if this.collectCountDown <= 0 then
    --    this.awardTime.text = "已无法领取";
    --else
    --    this.awardTime.text = HelperFunc.GetSurpTimeStr(this.collectCountDown);
    --end
    --this.collectCountDown = this.collectCountDown - 1;
end

function UIActivity.onClickTab(index)
    if this.lastIndex == index then
        return;
    end
    if this.isPlaying then
        this.allActiveTypeData[index].btn.selected = false;
        this.allActiveTypeData[this.lastIndex].btn.selected = true;
        return ;
    end
    this.lastIndex = index;
    this.nowIndex = 1;
    this.playEffect = true;
    this.initActivity(this.allActiveTypeData[index].type);
end

function UIActivity.onClickItem(context)
    local go = context.data;
    local childIndex = this.content:GetChildIndex(go) + 1;
    --if childIndex == this.nowIndex then
    --    return ;
    --end
    this.nowIndex = childIndex;
    this.onClickEnter();
    --this.updateBackGround(childIndex);
end

function UIActivity.onClickEnter()
    local data = this.ActiveBtnTab[this.nowIndex];
    if PanelNames[data.openUI] ~= nil then
        showUI(PanelNames[data.openUI], nil, data.activeId, this.onClickBack);
    else
        logError("活动找不到UI t_activitylist f_InterSurface:"..tostring(data.openUI));
    end
end
--返回
function UIActivity.onClickBack()
    closeUI(PanelNames.UIActivity);
    if this.needShowHeadMenu then
        showUI(PanelNames.UIHeadMenu);
        this.needShowHeadMenu = nil;
    end
end

return UIActivity;